package com.gzyj.server.factory.jtt808.up;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzyj.data.common.VehicleIncrement;
import com.gzyj.jtt808.utils.IntUtils;
import com.gzyj.jtt808.v2013.Package;
import com.gzyj.jtt808.v2013.enums.BusinessDataType;
import com.gzyj.jtt808.v2013.protocol.JTT808_0x0107;
import com.gzyj.pojo.web.yw.AnbiaoTerminal;
import com.gzyj.server.config.LogConfig;
import com.gzyj.server.factory.CommonHandler;
import com.gzyj.server.mapper.AnbiaoTerminalMapper;
import com.gzyj.server.service.CommonService;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
public class JTT808_0x0107_Handler implements CommonHandler {
    @Resource(name = "clusterRedisTemplate")
    private RedisTemplate<String, Object> clusterRedisTemplate;
    @Value("${redis.key.vehicle.cmdrs}")
    private String VehicleZdCMDRS;

    @Autowired
    private AnbiaoTerminalMapper anbiaoTerminalMapper;
    @Autowired
    private CommonService commonService;

    @Override
    public void handler(ChannelHandlerContext ctx, Package msg) throws Exception {
        JTT808_0x0107 jtt808_0x0107=(JTT808_0x0107) msg;
        VehicleIncrement vehicleInfo = commonService.getVehInfo(jtt808_0x0107.getPackageHeader().getTerminalPhoneNo());

        AnbiaoTerminal exist= anbiaoTerminalMapper.selectOne(new QueryWrapper<AnbiaoTerminal>().eq("vehId",vehicleInfo.getId()));
        if(exist!=null){
            exist.setVehId(vehicleInfo.getId());
            exist.setTerminalType(jtt808_0x0107.getTerminalType()+"");
            exist.setManufacturerId(jtt808_0x0107.getMakerId());
            exist.setTerminalModel(jtt808_0x0107.getTerminalModel());
            exist.setTerminalNumber(jtt808_0x0107.getTerminalId());
            exist.setTerminalIccid(jtt808_0x0107.getTerminal_SIM_ICCID());
            exist.setTerminalHardNumber(jtt808_0x0107.getTerminal_Hardware_Version_Num());
            exist.setTerminalGjNum(jtt808_0x0107.getTerminal_Firmware_Version_Num());
            exist.setGnssProp(IntUtils.intToBinary(Byte.toUnsignedInt(jtt808_0x0107.getGNSSModule()),8));
            exist.setTxProp(IntUtils.intToBinary(Byte.toUnsignedInt(jtt808_0x0107.getCommunicationModule()),8));
            anbiaoTerminalMapper.updateById(exist);
        }else{
            AnbiaoTerminal anbiaoTerminal=new AnbiaoTerminal();
            anbiaoTerminal.setVehId(vehicleInfo.getId());
            anbiaoTerminal.setTerminalType(jtt808_0x0107.getTerminalType()+"");
            anbiaoTerminal.setManufacturerId(jtt808_0x0107.getMakerId());
            anbiaoTerminal.setTerminalModel(jtt808_0x0107.getTerminalModel());
            anbiaoTerminal.setTerminalNumber(jtt808_0x0107.getTerminalId());
            anbiaoTerminal.setTerminalIccid(jtt808_0x0107.getTerminal_SIM_ICCID());
            anbiaoTerminal.setTerminalHardNumber(jtt808_0x0107.getTerminal_Hardware_Version_Num());
            anbiaoTerminal.setTerminalGjNum(jtt808_0x0107.getTerminal_Firmware_Version_Num());
            anbiaoTerminal.setGnssProp(IntUtils.intToBinary(Byte.toUnsignedInt(jtt808_0x0107.getGNSSModule()),8));
            anbiaoTerminal.setTxProp(IntUtils.intToBinary(Byte.toUnsignedInt(jtt808_0x0107.getCommunicationModule()),8));
            anbiaoTerminalMapper.insert(anbiaoTerminal);
        }
        clusterRedisTemplate.opsForValue().set(VehicleZdCMDRS + jtt808_0x0107.getPackageHeader().getTerminalPhoneNo() + ":" + 0x0107, jtt808_0x0107,10, TimeUnit.MINUTES);
        LogConfig.commondLog.info("808 2013版 {} 指令 解析完成", BusinessDataType.find(jtt808_0x0107.getPackageHeader().getMsgId()));
    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2019.Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2011.Package msg) {

    }

    @Override
    public void handler(VehicleIncrement vehicleInfo, String msg) {

    }
}
